package vn.conghuy.talkwithme.data.sqlmodel;

import java.util.ArrayList;

import android.database.Cursor;
import vn.conghuy.talkwithme.android.utils.DatabaseHelper;

public class WordModel {
	
	/**
	 * @uml.property  name="_id"
	 */
	public int _id;
	/**
	 * @uml.property  name="name"
	 */
	public String name;
	/**
	 * @uml.property  name="typeId"
	 */
	public int typeId;
	/**
	 * @uml.property  name="actionId"
	 */
	public int actionId;
	
	/**
	 * @uml.property  name="exType"
	 */
	public String exType;
	/**
	 * @uml.property  name="exAction"
	 */
	public String exAction;
	
	public WordModel(){}
	
	public WordModel(int _id){
		this._id = _id;
	}

	public WordModel(int _id, String name, int typeId, int actionId) {
		super();
		this._id = _id;
		this.name = name;
		this.typeId = typeId;
		this.actionId = actionId;
	}
	
	
	
	public WordModel(int _id, String name, int typeId, int actionId,
			String exType, String exAction) {
		super();
		this._id = _id;
		this.name = name;
		this.typeId = typeId;
		this.actionId = actionId;
		this.exType = exType;
		this.exAction = exAction;
	}

	public void receiveData(int _id){
		this._id = _id;
		String query = String.format("select Word.*, Action.name as ActionName, WordType.name as TypeName " + 
					"from Word, Action, WordType where Word._id = %d", _id);
		Cursor cursor = DatabaseHelper.GetInstance().ExecuteQuery(query);
		if(cursor.moveToFirst()){
			name = cursor.getString(cursor.getColumnIndex("name"));
			typeId = cursor.getInt(cursor.getColumnIndex("typeId"));
			actionId = cursor.getInt(cursor.getColumnIndex("actionId"));
			exAction = cursor.getString(cursor.getColumnIndex("ActionName"));
			exType = cursor.getString(cursor.getColumnIndex("TypeName"));
		}
		cursor.close();
	}
	
	public static ArrayList<WordModel> GetWordsByActionReturnModelList(String actionName){
		String query = String.format("select Word.*, Action.name as ActionName, WordType.name as TypeName " + 
				"from Word, Action, WordType where Word.typeId = WordType._id and "+
				"Word.actionId = Action._id and ActionName = \"%s\"", actionName);
		Cursor cursor = DatabaseHelper.GetInstance().ExecuteQuery(query);
		
		ArrayList<WordModel> words = new ArrayList<WordModel>();
		if(cursor.moveToFirst()){
			do{
				int _id = cursor.getInt(cursor.getColumnIndex("_id"));
				String name = cursor.getString(cursor.getColumnIndex("name"));
				int typeId = cursor.getInt(cursor.getColumnIndex("typeId"));
				int actionId = cursor.getInt(cursor.getColumnIndex("actionId"));
				String exAction = cursor.getString(cursor.getColumnIndex("ActionName"));
				String exType = cursor.getString(cursor.getColumnIndex("TypeName"));
				
				words.add(new WordModel(_id, name, typeId, actionId, exType, exAction));
				
			}while (cursor.moveToNext());
		
		}
		cursor.close();
		return words;
	}
	
	public static String[] GetWordsByAction(String action){
		ArrayList<WordModel> words = GetWordsByActionReturnModelList(action);
		String[] wordNames = new String[words.size()];
		int size = words.size();
		for(int i = 0; i < size; i++){
			wordNames[i] = words.get(i).name;
		}
		return wordNames;
	}
}
